﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Linq.Mapping;
using System.Runtime.Serialization;
using Portal.Data.Helper;
using Portal.Core;
using Portal.Core.Binding.FormBuilder;
using Portal.Core.Helper;
using System.Linq;
using Portal.Core.Utils;

namespace Portal.Data.Entity
{
    [Table(Name = "dbo.HistoryType"), Serializable, DataContractAttribute]
    public class HistoryType : BaseObject
    {
        #region Properties

        [SkipBinding]
        [Column(IsPrimaryKey = true, IsDbGenerated = true)]
        public int ID { get; set; }

        [Column]
        public string FullName { get; set; }

        [Column]
        public string SafeName { get; set; }

        #endregion

        #region Other properties
        public string Url
        {
            get
            {
                return string.Format(Const.UrlBookTypeFormat, CoreContext.basepath, SafeName, ID, CoreContext.UrlExtension);
            }
        }
        #endregion

        #region Methods
        public static HistoryType FindByPrimaryKey(object objectid)
        {
            return FindByPrimaryKey<HistoryType>(objectid);
        }

        public static List<HistoryType> ListAll()
        {
            //getcache  -----------------------
            var parameter = new object[] { typeof(HistoryType).Name, "ListAll" };
            var obj = CacheHelper.Instance.Get(parameter);
            if (obj != null)
                return (List<HistoryType>)obj;
            using (var context = new CmsDataContext())
            {
                var results = context.HistoryTypes.OrderBy(x => x.FullName).ToList();
                CacheHelper.Instance.Set(parameter, results);
                return results;
            }
        }

        public static HistoryType FindByName(string name)
        {
            var safeName = GetSafeName(name);
            //getcache  -----------------------
            var parameter = new object[] { typeof(HistoryType).Name, "FindByName", safeName };
            var obj = CacheHelper.Instance.Get(parameter);
            if (obj != null)
                return (HistoryType)obj;
            //end get cache -------------------

            var result = LINQHelper.GetFirst<HistoryType>("select * from HistoryType where SafeName={0}", safeName);
            CacheHelper.Instance.Set(parameter, result);
            return result;

        }
        /// <summary>
        /// Find or Insert to get ID from list Name
        /// </summary>
        public static List<int> GetIds(string types)
        {
            var results = new List<int>();
            var split = types.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var s in split)
            {
                var ht = FindByName(s);
                if (ht == null)
                {
                    ht = new HistoryType { FullName = s };
                    ht.Save();
                }
                results.Add(ht.ID);
            }
            return results;
        }
        #endregion Methods

        public override void BeforeSaveUpdate()
        {
            base.BeforeSaveUpdate();
            SafeName = GetSafeName(FullName);
        }
    }
}
